package com.se.io;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileCodeTypeUtil {
    /**
     * 获取文件的的编码格式
     *
     * @param fileName
     * @return
     * @throws IOException
     */
    public static String getFileCodeType(String fileName) throws IOException {
        BufferedInputStream bin;
        int bom = 0;
        String str = " ";
        String str2 = "";
        try {
            bin = new BufferedInputStream(new FileInputStream(fileName));
            bom = (bin.read() << 8) + bin.read();
            // 获取两个字节内容，如果文件无BOM信息，则通过判断字的字节长度区分编码格式
            byte bs[] = new byte[10];
            while (str.matches("\\s+\\w*")) {
                bin.read(bs);
                str = new String(bs, "UTF-8");
            }
            str2 = new String(bs, "GBK");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        String code = null;
        // 有BOM
        switch (bom) {
            case 0xefbb:
                code = "UTF-8";
                break;
            case 0xfffe:
                code = "Unicode";
                break;
            case 0xfeff:
                code = "UTF-16BE";
                break;
            case 0x6100:
                code = "UTF-16LE";
                break;
            case 0x5c75:
                code = "ANSI|ASCII";
                break;
            default:
                // 无BOM
                if (str.length() <= str2.length()) {
                    code = "UTF-8";
                } else {
                    code = "GBK";
                }
        }


        return code;
    }
}




